<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"chenwingsing.github.io","root":"/","images":"/images","scheme":"Mist","darkmode":false,"version":"8.8.2","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="局域网控制不够爽！">
<meta property="og:type" content="article">
<meta property="og:title" content="HomeAassistant系列（一）外网远程控制">
<meta property="og:url" content="https://chenwingsing.github.io/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/index.html">
<meta property="og:site_name" content="小陈同学">
<meta property="og:description" content="局域网控制不够爽！">
<meta property="og:locale">
<meta property="article:published_time" content="2022-01-02T13:37:03.000Z">
<meta property="article:modified_time" content="2022-01-03T11:43:09.201Z">
<meta property="article:author" content="小陈同学">
<meta property="article:tag" content="cpolar">
<meta property="article:tag" content="远程控制">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://chenwingsing.github.io/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://chenwingsing.github.io/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/","path":"2022/01/02/HomeAassistant系列（一）外网远程控制/","title":"HomeAassistant系列（一）外网远程控制"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>HomeAassistant系列（一）外网远程控制 | 小陈同学</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="小陈同学" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">小陈同学</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-books"><a href="/books/" rel="section"><i class="fa fa-book fa-fw"></i>书籍</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">小陈同学</p>
  <div class="site-description" itemprop="description">求知若饥，虚心若愚</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/chenwingsing" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chenwingsing" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:chen_yongcheng@outlook.com" title="E-Mail → mailto:chen_yongcheng@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>RSS</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://chenwingsing.github.io/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="小陈同学">
      <meta itemprop="description" content="求知若饥，虚心若愚">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小陈同学">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          HomeAassistant系列（一）外网远程控制
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-02 21:37:03" itemprop="dateCreated datePublished" datetime="2022-01-02T21:37:03+08:00">2022-01-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-01-03 19:43:09" itemprop="dateModified" datetime="2022-01-03T19:43:09+08:00">2022-01-03</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/HomeAassistant/" itemprop="url" rel="index"><span itemprop="name">HomeAassistant</span></a>
        </span>
    </span>

  
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">Valine：</span>
  
    <a title="valine" href="/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.7k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>2 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>局域网控制不够爽！</p>
<span id="more"></span>
<p>在<a target="_blank" rel="noopener" href="https://mrchangg.github.io/2021/12/27/%E6%A0%91%E8%8E%93%E6%B4%BE%E4%B8%8D%E5%90%83%E7%81%B0%E8%A1%8C%E5%8A%A8/#%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F">这篇文章</a>中已经讲了如何进行内网穿透来远程连接我们的树莓派，那么如果能够让Homeassistant也能够进行远程连接的话，那么这样会很爽。</p>
<p>我是在Docker下安装的HA，在设置外网远程连接的时候遇到了非常多的坑，下面先说下我遇到的坑。</p>
<p>1.Docker下有4中network模式，有host，container，none，bridge（默认），一开始按照别人的教程安装是<code>bridge</code>模式，然后用<code>cpolar http 8123</code>进行内网穿透，遇到了我的第一个坑，页面显示400 bad request。<br><strong>解决方案</strong>：凭借着不解决不死心的决心，我搜索了各种资料，终于让我在bing上找到了方法，由于是HA是在docker下运行的，所以有一个虚拟的IP地址，可以用portainer找到网络模式下找到这个IP地址，显示为172.17.0.1，解决的方案就是在<code>configuration.yaml</code>下添加下面的信息：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">http:</span><br><span class="line">  use_x_forwarded_for: true</span><br><span class="line">  trusted_proxies:</span><br><span class="line">    - 172.17.0.1</span><br></pre></td></tr></table></figure>
<p>然后重新进行内网穿透，哇！居然可以了！可是还有坑迎接我😭</p>
<p>2.然后我开始配置homekit，非常nice，有官方集成，直接一键傻瓜操作，并且生成了二维码让我去扫，结果我试了好几次，都说无法找到设备，我服了。<br><strong>解决方案</strong>：凭借着不解决不死心的决心，我搜索了各种资料，终于又让我在bing(必应还是不错的)上找到了方法，因为我们安装的时候docker选择bridge模式，这种网络homekit是无法找到设备的，需要设置为<code>host</code>模式，ok，行，那我删除镜像重新安装，也是非常简单的，把bridge改成host。没错，真的就是这么一回事，果然找到设备了。然后我又兴高采烈用<code>cpolar http 8123</code>穿透，结果显示400 bad request，可是这次是host模式呀，和宿主机共享呀，根本不是bridge下那种情况，所以我就在配置文件写了我的本机地址192.168.1.104，结果还是不行，难度homekit和远程连接我只能选择其中一样？可是我都想要呀，然后我又想了半天，试试能不能用nginx进行反向代理，设置了好久也没有搞定，终于最后，我查了两天资料，又让我在论坛上找到了别人的一个配置信息，只需要在<code>configuration.yaml</code>添加这个配置就可以。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">http:</span><br><span class="line">  server_port: 8123</span><br><span class="line">  use_x_forwarded_for: True</span><br><span class="line">  trusted_proxies:</span><br><span class="line">    - 127.0.0.1</span><br><span class="line">    - ::1</span><br><span class="line">    - 10.0.20.0/24</span><br><span class="line">    - 10.10.0.0/16</span><br><span class="line">    - 10.43.0.0/16</span><br><span class="line">    - 10.42.0.0/16</span><br><span class="line">    - 10.0.20.81</span><br><span class="line">    - 10.0.20.82</span><br><span class="line">    - 10.0.20.83</span><br><span class="line">    - 10.42.1.14</span><br><span class="line">    - 10.42.0.42</span><br><span class="line">  ip_ban_enabled: False</span><br><span class="line">  login_attempts_threshold: 5</span><br></pre></td></tr></table></figure>
<p>成功了！homekit和远程我都可以一起要了！这里有个细节，就是cpolar会生成两个链接，一个是加密的https，一个是http，因为咱是白嫖，没有域名，没有服务器，所以只能用http这个，当然，如果你有域名有云服务器的话，应该不会遇到我上面这些问题，因为我看网络上远程链接教程还是比较全的。</p>
<p><strong>一些提示</strong><a target="_blank" rel="noopener" href="https://www.cpolar.com/">cpolar</a>和<a target="_blank" rel="noopener" href="https://ngrok.cc/">ngork.cc</a>都可以用免费的内网穿透，区别在于cpolar是动态更换链接，而ngrok链接可以是固定的，但是太卡了，所以我还是选择cpolar，当然，有钱任性，可以直接买他们的服务。</p>
<p><strong>一些心得：</strong>其实在bridge模式下能够进行远程连接，我就已经有信心在host模式下远程连接，所以我觉得肯定是一些配置上出现了问题，后来想到用nginx进行反向代理，虽然我并没有成功设置好，但是我觉得即便设置好了，也会出现400，因为本质上不是这个问题，而是HA直接拒绝你，所以后来不从这个思路下手，终于更换各种关键词去论坛上搜索，找到一个人的配置，太开心了，还有，英文还是得要学好，HA论坛都是英文。</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>小陈同学
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://chenwingsing.github.io/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/" title="HomeAassistant系列（一）外网远程控制">https://chenwingsing.github.io/2022/01/02/HomeAassistant系列（一）外网远程控制/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/cpolar/" rel="tag"><i class="fa fa-tag"></i> cpolar</a>
              <a href="/tags/%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/" rel="tag"><i class="fa fa-tag"></i> 远程控制</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/12/27/%E6%A0%91%E8%8E%93%E6%B4%BE%E4%B8%8D%E5%90%83%E7%81%B0%E8%A1%8C%E5%8A%A8/" rel="prev" title="树莓派不吃灰行动">
                  <i class="fa fa-chevron-left"></i> 树莓派不吃灰行动
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/01/10/Docker%E4%B8%8B%E5%AE%89%E8%A3%85nginx-php/" rel="next" title="Docker下安装nginx和php">
                  Docker下安装nginx和php <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="valine-comments"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小陈同学呀</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">83k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">1:15</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  






<script class="next-config" data-name="valine" type="application/json">{"enable":true,"appId":"5kJQPbqUaVp4R03CwsPV0PEx-gzGzoHsz","appKey":"alfUMJjgEoEg26GBrn4BfzwM","serverURLs":"https://5kjqpbqu.lc-cn-n1-shared.com","placeholder":"上面的信息均为选填，快来留下你的脚印😎 Tips：此匿名评论系统支持Gravatar以及QQ头像(昵称输入您的QQ号)","avatar":"mp","meta":["nick","mail","link"],"pageSize":10,"lang":null,"visitor":false,"comment_count":false,"recordIP":true,"enableQQ":true,"requiredFields":[],"el":"#valine-comments","path":"/2022/01/02/HomeAassistant%E7%B3%BB%E5%88%97%EF%BC%88%E4%B8%80%EF%BC%89%E5%A4%96%E7%BD%91%E8%BF%9C%E7%A8%8B%E6%8E%A7%E5%88%B6/"}</script>
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.valine.el)
    .then(() => NexT.utils.getScript(
      'https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js',
      { condition: window.Valine }
    ))
    .then(() => {
      new Valine(CONFIG.valine);
    });
});
</script>

</body>
</html>
